package com.song.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.song.dao.SequenceDao;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Description: SpringConfig：类描述
 * @author: songzhangyong
 * @Date: 2019/11/9 21:15
 */

@Configuration
@ComponentScan("com.song")
@MapperScan("com.song.dao")
public class SpringConfig {


    @Autowired
    private SequenceDao sequenceDao ;

    public SequenceDao getSequenceDao() {
        return sequenceDao;
    }

    public void setSequenceDao(SequenceDao sequenceDao) {
        this.sequenceDao = sequenceDao;
    }

    /**
     * 整合第三方bean
     * @return
     */
    @Bean
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
//        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds.setUrl("jdbc:mysql://47.93.62.76:3306/song_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&serverTimezone=UTC");
        ds.setUsername("root");
        ds.setPassword("1QAZ2wsx");
        return ds;
    }


    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        return factoryBean.getObject();
    }


}
